/**
 * Created by thomas on 2017/8/11.
 */
'use strict';

import Error_log from '../../models/error_log/error_log'
import AddressComponent from '../../prototype/addressComponent'

import formidable from 'formidable'//载入 formidable 处理post的插件


class Admin extends AddressComponent {
    constructor(){
        super()//继承
        this.get_error_log = this.get_error_log.bind(this)//登陆
    }
    async get_error_log(req, res, next){
        const form = new formidable.IncomingForm();
        form.parse(req, async (err, fields, files) => {
            if (err) {
                res.send({
                    status: 0,
                    type: 'FORM_DATA_ERROR',
                    message: '表单信息错误'
                })
                return
            }
            const {time, error_log} = fields;//取值
            console.log("time"+time)
            console.log("error_log"+error_log)
            try {
                if (!time) {
                    throw new Error('没有提交时间');
                } else if (!error_log) {
                    throw new Error('没有提交错误信息');
                }
            } catch (err) {
                console.log(err.message, err);
                res.send({
                    status: 0,
                    type: 'GET_ERROR_PARAM',
                    message: err.message,
                })
                return
            }
            //插入数据
            try{
                const newAdmin = {
                    Time:time,
                    error_log:error_log
                };
                await Error_log.create(newAdmin);
                console.log("cha ru_+_+_+")
            }catch(e){
                console.log(e)
            }
        })
    }
}

export default new Admin()